package com.epam.cdp.web;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.epam.cdp.ejb.statefull.UserSession;
import com.epam.cdp.ejb.utilities.GadgetWindow;
import com.epam.cdp.ejb.utilities.Position;
import com.epam.cdp.ejb.utilities.exceptions.GadgetWindowDoesntExists;

public class StatefullExampleBackingBean implements UserSession {
	
	private static final transient Log log = LogFactory.getLog(StatefullExampleBackingBean.class);
	
	private String newWindowName;
	private int newWindowXPosition;
	private int newWindowYPosition;
	
	@EJB
	private UserSession userSession;


	@PostConstruct
	public void postConstruct() {
		log.info("StatefullExampleBackingBean created");
		userSession.openNewWindow("greetingWindow", new Position(10, 10));
		userSession.openNewWindow("loginWindow", new Position(20, 30));
	}

	@Override
	public GadgetWindow openNewWindow(String name, Position position) {
		return userSession.openNewWindow(name, position);
	}

	@Override
	public void closeWindow(String name) throws GadgetWindowDoesntExists {
		log.info("Clossing window " + name);
		userSession.closeWindow(name);
		log.info(getAllWindows());
	}

	@Override
	public List<GadgetWindow> getAllWindows() {
		return userSession.getAllWindows();
	}

	@Override
	public GadgetWindow getByName(String name) {
		return userSession.getByName(name);
	}

	public void addWindow() {
		userSession.openNewWindow(newWindowName, new Position(newWindowXPosition, newWindowYPosition));
		newWindowName = "";
		newWindowXPosition = 0;
		newWindowYPosition = 0;
	}
	
	public void clossAll() {
		userSession.getAllWindows().clear();
	}
	
	public UserSession getUserSession() {
		return userSession;
	}
	
	public void setUserSession(UserSession userSession) {
		this.userSession = userSession;
	}
	
	public int getNewWindowXPosition() {
		return newWindowXPosition;
	}
	
	public void setNewWindowXPosition(int newWindowXPossition) {
		this.newWindowXPosition = newWindowXPossition;
	}
	
	public int getNewWindowYPosition() {
		return newWindowYPosition;
	}
	
	public void setNewWindowYPosition(int newWindowYPossition) {
		this.newWindowYPosition = newWindowYPossition;
	}
	
	public String getNewWindowName() {
		return newWindowName;
	}
	
	public void setNewWindowName(String newWindowName) {
		this.newWindowName = newWindowName;
	}
}
